package formulation.controllers;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import db.formulation.Formulation;
import db.formulation.FormulationService;
import db.fortifier.Fortifier;
import db.fortifier.FortifierService;
import db.vector.Vector;
import db.vector.VectorService;

public class FormulationController extends MultiActionController
{
    private FortifierService fortifierService;
    private FormulationService formulationService;
    private VectorService vectorService;
    
    public void setFortifierService(FortifierService fortifierService) {
        this.fortifierService = fortifierService;
    }
    
    public void setFormulationService(FormulationService formulationService) {
        this.formulationService = formulationService;
    }
    
    public void setVectorService(VectorService vectorService) {
        this.vectorService = vectorService;
    }
    // administrator methods
    public ModelAndView admin(HttpServletRequest request,
            HttpServletResponse response)
    {
        request.setAttribute("fortifiers", fortifierService.list());
        return new ModelAndView("formulation/admin/formulation", "formulations", formulationService.list());
    }
    
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response, Formulation formulation)
    {
        formulationService.insert(formulation);
        return new ModelAndView("redirect:admin.do");
    }
    
    public ModelAndView remove(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        formulationService.remove(id);
        return new ModelAndView("redirect:admin.do");
//        return new ModelAndView("redirect:../frame/formulation_manage_form.do");
    }
    // end
    public ModelAndView listFortifier(HttpServletRequest request,
            HttpServletResponse response, Fortifier fortifier)
    {
        Map<Fortifier, Double> fortifiers = formulationService.listFortifier(fortifier.getId());
        return new ModelAndView("formulation/show-fortifier", "fortifiers", fortifiers);
    }
    
    public ModelAndView addFortifier(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long formulationId = Long.valueOf(request.getParameter("formulationId"));
        Long fortifierId = Long.valueOf(request.getParameter("fortifierId"));
        Double mass = Double.valueOf(request.getParameter("mass"));
        formulationService.addFortifier(formulationId, fortifierId, mass);
        return new ModelAndView("redirect:list.do");
    }
    
    public ModelAndView newFormulation(HttpServletRequest request,
            HttpServletResponse response, Formulation formulation)
    {
    	formulationService.insert(formulation);
    	return new ModelAndView("redirect:../frame/formulation_manage_form.do");
    }
    
    public ModelAndView newEditFormulation(HttpServletRequest request,
            HttpServletResponse response, Formulation formulation)
    {
    	formulationService.insert(formulation);
    	return new ModelAndView("redirect:../frame/formulation_edit_form.do");
    }
    
    public ModelAndView deleteManageFormulation(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        formulationService.remove(id);
        return new ModelAndView("redirect:../frame/formulation_manage_form.do");
    }
    
    public ModelAndView viewPDF(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        Formulation formulation = formulationService.find(id);

        request.setAttribute("formulation", formulation);
        return new ModelAndView("pdfViewer");
    }
    
    public ModelAndView addFormulation(HttpServletRequest request,
            HttpServletResponse response) throws JSONException
    {
        String strJson = request.getParameter("json");
        JSONObject jsonObj = new JSONObject(strJson);
        
        String name = jsonObj.getString("name");
        Long vectorId = jsonObj.getLong("vid");
        Vector vector = vectorService.find(vectorId);
        
        JSONArray jsonArr = jsonObj.getJSONArray("fortifiers");
        
        HashMap<Fortifier, Double> fortifiers = new HashMap<Fortifier, Double>();
        for (int i = 0; i < jsonArr.length(); i++) {
            JSONObject item = jsonArr.getJSONObject(i);
            Long fortifierId = item.getLong("id");
            Fortifier fortifier = fortifierService.find(fortifierId);
            fortifiers.put(fortifier, item.getDouble("mass"));
        }
        Formulation formulation = new Formulation();
        formulation.setName(name);
        formulation.setVector(vector);
        formulation.setFortifiers(fortifiers);
        formulationService.insert(formulation);
        return new ModelAndView("redirect:/frame/formulation_manage_form.do");
    }
}
